/*
 * Android Tutorials - Mixing HelloDatePicker with HelloTimePicker into one application
 * 
 * Author: Nektarios Ioannides
 * 
 */

///------------------------------------------------------------------------------------

package com.example.hellodatetimepicker;

import java.util.Calendar;

import com.example.hellodatetimepicker.R;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

///------------------------------------------------------------------------------------


public class HelloDateTimePicker extends Activity {
	private TextView mDateDisplay;
    private TextView mTimeDisplay;
    
	private Button mPickDate;
    private Button mPickTime;

    private int mYear;
    private int mMonth;
    private int mDay;
  
    private int mHour;
    private int mMinute;

    static final int DATE_DIALOG_ID = 0;
    static final int TIME_DIALOG_ID = 1;

  ///------------------------------------------------------------------------------------

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // capture our View elements
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        mPickDate = (Button) findViewById(R.id.pickDate);
        mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
        mPickTime = (Button) findViewById(R.id.pickTime);


        // add a click listener to the buttons
        mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
        
        mPickTime.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDialog(TIME_DIALOG_ID);
			}
		});

        // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        // display the current date and time
        updateDisplay(DATE_DIALOG_ID);
        updateDisplay(TIME_DIALOG_ID);
    }


    ///------------------------------------------------------------------------------------
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);
        
        case TIME_DIALOG_ID:
        	return new TimePickerDialog(this,
        			mTimeSetListener, mHour, mMinute, false);
    	}
        return null;
    }

    ///------------------------------------------------------------------------------------
    
    // updates the date we display in the TextView
    private void updateDisplay(int nTextViewType) {
    	
    	//For DatePicker
    	switch (nTextViewType) {
    	case DATE_DIALOG_ID :	
	        mDateDisplay.setText(
	            new StringBuilder()
	                    // Month is 0 based so add 1
	                    .append(mMonth + 1).append("-")
	                    .append(mDay).append("-")
	                    .append(mYear).append(" "));
    	
    	case TIME_DIALOG_ID	:
	        //For TimePicker
	        mTimeDisplay.setText(
	        		new StringBuilder()
	                	.append(pad(mHour)).append(":")
	                    .append(pad(mMinute)));
	    }
    }
    ///------------------------------------------------------------------------------------

    // the callback received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener =
	        new DatePickerDialog.OnDateSetListener() {
	
	            public void onDateSet(DatePicker view, int year, 
	                                  int monthOfYear, int dayOfMonth) {
	                mYear = year;
	                mMonth = monthOfYear;
	                mDay = dayOfMonth;
	                updateDisplay(DATE_DIALOG_ID);
	            }
	        };
	        
    ///------------------------------------------------------------------------------------

    // the callback received when the user "sets" the time in the dialog
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;
                updateDisplay(TIME_DIALOG_ID);
            }
        };

    ///------------------------------------------------------------------------------------

    // pad() method for TimePicker for padding-ip 0's when a number is a single digit
    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

    ///------------------------------------------------------------------------------------

}

///------------------------------------------------------------------------------------

//end of file